package com.studentstreet.adapter;

/*
 * 用于显示各个商家的GridView
 * 作者: 李文涛 
 * 时间：2012-10-5
 */

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.studentstreet.R;
import com.studentstreet.entities.SellerEntity;
import com.studentstreet.tools.SyncImageLoader;

public class GridViewAdapter extends BaseAdapter implements ListAdapter {

	final String TAG = "GridViewAdapter";
	private LayoutInflater mInflater;
	private Vector<SellerEntity> mModels = new Vector<SellerEntity>();
	private SyncImageLoader syncImageLoader;
	private Context context;
	private GridView mGridView;

	public GridViewAdapter(Context context) {
		this.context = context;
	}

	public GridViewAdapter(Context context, GridView mGridView) {
		mInflater = LayoutInflater.from(context);
		syncImageLoader = new SyncImageLoader();
		this.context = context;
		this.mGridView = mGridView;
	}

	public void clean() {
		mModels.clear();
	}

	public void addSeller(String seller_name, String image_url, String tel) {
		SellerEntity seller = new SellerEntity();
		seller.setName(seller_name);
		seller.setLogoURL(image_url);
		seller.setTel(tel);
		mModels.add(seller);
	}

	public void addSeller(SellerEntity seller) {
		mModels.add(seller);
	}

	@Override
	public int getCount() {
		return this.mModels.size();
	}

	@Override
	public Object getItem(int position) {
		if (position >= getCount()) {
			return null;
		}
		return mModels.get(position);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.seller_item, null);
		}
		final SellerEntity seller = mModels.get(position);
		convertView.setTag(position);
		ImageView iv = (ImageView) convertView
				.findViewById(R.id.seller_item_ImageView);
		TextView sItemName = (TextView) convertView
				.findViewById(R.id.seller_name_TextView);
		ImageView infoIv = (ImageView) convertView
				.findViewById(R.id.seller_info);
		infoIv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showPopup(v, seller);
			}
		});
		sItemName.setText(seller.getName());
		iv.setBackgroundResource(R.drawable.goods_item_bg);
		syncImageLoader.loadImage(position, seller.getLogoURL(),
				imageLoadListener);
		return convertView;
	}

	private void showPopup(View v, SellerEntity seller) {
		PopupWindow popupWindow = null;
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的宽度的一半
		int width = windowManager.getDefaultDisplay().getWidth();
		int height = windowManager.getDefaultDisplay().getHeight();
		if (popupWindow == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			View view = layoutInflater.inflate(R.layout.seller_info, null);
			// 创建一个PopuWidow对象
			popupWindow = new PopupWindow(view, width / 2,
					android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			TextView nameTv = (TextView) view.findViewById(R.id.seller_name_tv);
			nameTv.setText(seller.getName());
			TextView phoneTv = (TextView) view
					.findViewById(R.id.seller_phone_tv);
			phoneTv.setText(seller.getTel());
			TextView descTv = (TextView) view.findViewById(R.id.seller_desc_tv);
			descTv.setText(seller.getDesc());
		}
		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		// 显示的位置为:根据LorR的值来确定
		// popupWindow
		popupWindow.showAsDropDown(v);
		/*
		 * if (LorR % 2 == 0) { System.out.println(v.getWidth() + "--->");
		 * popupWindow.showAtLocation(v, Gravity.RIGHT, 0, 0); } else {
		 * popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0); }
		 */
	}

	SyncImageLoader.OnImageLoadListener imageLoadListener = new SyncImageLoader.OnImageLoadListener() {

		@Override
		public void onImageLoad(Integer t, Drawable drawable) {
			View view = mGridView.findViewWithTag(t);
			if (view != null) {
				ImageView iv = (ImageView) view
						.findViewById(R.id.seller_item_ImageView);
				iv.setBackgroundDrawable(drawable);
			}
		}

		@Override
		public void onError(Integer t) {
			SellerEntity model = (SellerEntity) getItem(t);
			View view = mGridView.findViewWithTag(model);
			if (view != null) {
				ImageView iv = (ImageView) view
						.findViewById(R.id.seller_item_ImageView);
				iv.setBackgroundResource(R.drawable.goods_item_bg);
			}
		}

	};
}
